package com.itheima.case2.web;

import com.itheima.case2.pojo.vo.*;
import com.itheima.case2.service.UserServiceImpl;
import com.itheima.case2.utils.BaseController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {


    /*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求和响应编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //1.获取方法名
        String url = request.getRequestURI();
        int lastIndex = url.lastIndexOf('/');
        String method = url.substring(lastIndex + 1,url.length());

        if(method.equals("query")) {
            //分页查询用户功能
            this.query(request,response);
        }else if(method.equals("add")) {
            //添加用户数据功能
            add(request,response);
        } else if(method.equals("update")) {
            //修改用户数据功能
            update(request,response);
        } else if(method.equals("delete")) {
            //删除用户数据功能
            delete(request,response);
        }

    }*/

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Integer id = Integer.parseInt(request.getParameter("id"));
            UserServiceImpl userServiceImpl = new UserServiceImpl();

            userServiceImpl.deleteUser(id);
            // 3 响应
            BaseController.printResult(response, new Result(true, "删除用户成功!"));
        } catch (RuntimeException e) {
            e.printStackTrace();
            BaseController.printResult(response, new Result(false, "删除用户失败! 原因:" + e.getMessage()));
        }
    }
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            UpdateUser user = BaseController.parseJSON2Object(request, UpdateUser.class);

            UserServiceImpl courseService = new UserServiceImpl();
            courseService.updateUser(user);

            BaseController.printResult(response, new Result(true, "修改用户成功!"));
        } catch (RuntimeException e) {
            e.printStackTrace();
            BaseController.printResult(response, new Result(false, "修改用户失败!"));
        }
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            AddUser user = BaseController.parseJSON2Object(request, AddUser.class);
            System.out.println(user);

            UserServiceImpl service = new UserServiceImpl();
            service.addUser(user);

            BaseController.printResult(response,new Result(true,"添加用户成功"));
        } catch (Exception e) {
            e.printStackTrace();
            BaseController.printResult(response, new Result(false, "添加用户失败!"));
        }
    }

    protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 1 获取参数 封装到 queryPageBean中
            QueryPageBean queryPageBean = BaseController.parseJSON2Object(request, QueryPageBean.class);
            if(queryPageBean==null) {
                //如果获取不到,则设置默认初始值
                queryPageBean = new QueryPageBean();
                queryPageBean.setCurrentPage(1);
                queryPageBean.setPageSize(5);
            }
//            queryPageBean =  new QueryPageBean();

            // 2 调用业务层 查询分页结果
            UserServiceImpl courseService = new UserServiceImpl();
            PageResult pageResult = courseService.findListByPage(queryPageBean);

            // 3 响应
            BaseController.printResult(response, new Result(true, "查询成功!", pageResult));
        } catch (RuntimeException e) {
            e.printStackTrace();
            BaseController.printResult(response, new Result(false, "查询失败!"));
        }
    }

}